﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/
using System;
using System.ComponentModel;
using Newtonsoft.Json;

namespace OI.DataModel
{
    public class OIDataset : IEditableObject, IDataErrorInfo 
    {
        public string Name
        {
            get 
            {
                return _Name;
            }
            set 
            { 
                _Name = value; 
            }
        }
        [JsonIgnore]
        private string _Name = "[Unnamed]";

        public int ID { get; set; }
        public int JurisdictionFK { get; set; }

        public int CategoryID { get; set; }
        public int SourceID { get; set; }
        public string CategoryName { get; set; }
        public string SourceName { get; set; }
        public string SourceFilter { get; set; }
        public string Description { get; set; }

        public DateTime PublishedDate
        {
            get { return _PublishedDate; }
            set { _PublishedDate = value; }
        }
        [JsonIgnore]
        private DateTime _PublishedDate = DateTime.Now;

        [JsonIgnore]
        public string FormattedPublishedDate
        {
            get
            {
                var noDate = new DateTime(1900, 1, 1);
                return (PublishedDate <= noDate) ? "not published" : PublishedDate.ToShortDateString();
            }
        }

        public string ContactEmail { get; set; }
        public string Tags { get; set; }
        public string UpdateFrequency { get; set; }
        public string TableName { get; set; }
        public string LayerID { get; set; }
        public bool IsPublished { get; set; }
        public bool IsSpatial{ get; set; }


        // support undo in editors
        [JsonIgnore]
        private string Checkpoint { get; set; }

        [JsonIgnore]
        private bool InEdit { get; set; }

        [JsonIgnore]
        public Action PostCancelAction { get; set; }

        [JsonIgnore]
        public Func<OIDataset, string, string> PostValidationFunction { get; set; }

        public void BeginEdit()
        {
            Checkpoint = MessageSerializer.Serialize(this);
            InEdit = true;
        }

        public void CancelEdit()
        {
            if (!string.IsNullOrEmpty(Checkpoint))
            {
                var checkpoint = MessageSerializer.Deserialize(Checkpoint) as OIDataset;
                if (checkpoint != null)
                {
                    ID = checkpoint.ID;
                    CategoryID = checkpoint.CategoryID;
                    SourceID = checkpoint.SourceID;
                    Name = checkpoint.Name;
                    CategoryName = checkpoint.CategoryName;
                    SourceName = checkpoint.SourceName;
                    SourceFilter = checkpoint.SourceFilter;
                    PublishedDate = checkpoint.PublishedDate;
                    Description = checkpoint.Description;
                    ContactEmail = checkpoint.ContactEmail;
                    Tags = checkpoint.Tags;
                    UpdateFrequency = checkpoint.UpdateFrequency;
                    TableName = checkpoint.TableName;
                    LayerID = checkpoint.LayerID;
                    IsPublished = checkpoint.IsPublished;
                    IsSpatial = checkpoint.IsSpatial;
                }
            }

            if (PostCancelAction != null)
            {
                PostCancelAction();
            }

            InEdit = false;
        }

        public void EndEdit()
        {
            InEdit = false;
        }

        public string Error
        {
            get { return null; }
        }

        // data validation
        public string this[string columnName]
        {
            get
            {
                // only validate when we are editing
                if (!InEdit)
                {
                    return null;
                }

                switch (columnName)
                {
                    case "Name":
                        if (string.IsNullOrEmpty(Name))
                        {
                            return "must be provided for a dataset!";
                        }
                        break;
                    case "TableName":
                        if (string.IsNullOrEmpty(TableName))
                        {
                            return "must be provided for a dataset!";
                        }
                        break;
                    case "LayerID":
                        if (string.IsNullOrEmpty(LayerID) && IsSpatial)
                        {
                            return "cannot be empty if this is a spatial layer!";
                        }
                        break;
                    case "PublishedDate":
                        if (PublishedDate.Year <= 1900)
                        {
                            return "must be a valid date after 1900";
                        }
                        break;
                }

                if (PostValidationFunction != null)
                {
                    return PostValidationFunction(this, columnName);
                }
                return null;
            }
        }
    }
}